package src.反射2;

/*
反射调用方法
 */
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestBoos {
    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, ClassNotFoundException, InstantiationException {
        //1.得到反射类对象
        Class<?> aClass = Class.forName("反射2.Boos");
        //2.创建反射类对象
        Object o = aClass.newInstance();
        //调用boos中的say方法(private)
        Method say = aClass.getDeclaredMethod("Say", int.class, String.class, char.class);
        //关闭访问权限
        say.setAccessible(true);
        //反射调用方法invoke
        Object invoke = say.invoke(o,200,"王五",'男');
        System.out.println("invoke = " + invoke);

        //调用公共的hi方法
        Method hi = aClass.getDeclaredMethod("hi", String.class);
        Object invoke1 = hi.invoke(o, "小米之家");
        System.out.println("invoke1 = " + invoke1);

        //调用mi方法
        Method m1 = aClass.getDeclaredMethod("m1");
        Object invoke2 = m1.invoke(o);
        System.out.println("invoke2 = " + invoke2);
    }
}
